package com.cskaoyan.service.impl;

import com.cskaoyan.bean.bo.OrderListBO;
import com.cskaoyan.bean.bo.OrderRefundBO;
import com.cskaoyan.bean.bo.OrderShipBO;
import com.cskaoyan.bean.vo.OrderDetailVO;
import com.cskaoyan.bean.vo.OrderListVO;
import com.cskaoyan.bean.pojo.MallOrder;
import com.cskaoyan.bean.pojo.MallOrderExample;
import com.cskaoyan.bean.pojo.MallOrderGoods;
import com.cskaoyan.bean.pojo.MallUser;
import com.cskaoyan.mapper.MallOrderGoodsMapper;
import com.cskaoyan.mapper.MallOrderMapper;
import com.cskaoyan.mapper.MallUserMapper;
import com.cskaoyan.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *@author: ycb
 *@description: 订单管理
 *@time: 2021/7/8 22:05
 *
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    MallOrderMapper orderMapper;
    @Autowired
    MallUserMapper userMapper;
    @Autowired
    MallOrderGoodsMapper orderGoodsMapper;

    /**
     * @Author ycb
     * @description: 订单退款
     * @Date 11:35 2021/7/9
     * url:   http://182.92.235.201:8083/admin/order/refund
     */
    @Override
    public void refundMoney(OrderRefundBO refundBO) {
        MallOrder order = new MallOrder();
        order.setOrderStatus(203);

        MallOrderExample mallOrderExample = new MallOrderExample();
        MallOrderExample.Criteria criteria = mallOrderExample.createCriteria();
        criteria.andIdEqualTo(refundBO.getOrderId());

        orderMapper.updateByExampleSelective(order,mallOrderExample);
    }

    /**
     * @Author ycb
     * @description: 订单发货
     * @Date 11:50 2021/7/9
     * url:      http://182.92.235.201:8083/admin/order/ship
     */
    @Override
    public Integer orderShip(OrderShipBO shipBO) {
        int effectedRows = 0;
        if (shipBO.getShipSn() != null && shipBO.getShipChannel() != null) {
           effectedRows  = orderMapper.updateShipInfo(shipBO.getOrderId(), shipBO.getShipSn(), shipBO.getShipChannel(), new Date());
        }
        return effectedRows;
    }

    /**
     * @Author ycb
     * @description: 获取订单详情
     * @Date 10:44 2021/7/9
     * url: http://182.92.235.201:8083/admin/order/detail?id=20
     */
    @Override
    public OrderDetailVO getOrderDetail(Integer id) {
        MallOrder order = orderMapper.selectByPrimaryKey(id);

        OrderDetailVO orderDetailVO = new OrderDetailVO();
//        MallOrder order1 = orderMapper.selectByPrimaryKey(order.getId());
        MallUser user = userMapper.selectByPrimaryKey(order.getUserId());
        List<MallOrderGoods> orderGoodsList = orderGoodsMapper.selectByUserId(order.getId());

        orderDetailVO.setOrder(order);
        orderDetailVO.setUser(user);
        orderDetailVO.setOrderGoods(orderGoodsList);

        return orderDetailVO;
    }

    /**
     * @Author ycb
     * @description: 获取订单列表
     * @Date 10:26 2021/7/9
     * url:
     */
    @Override
    public OrderListVO getList(OrderListBO listBO) {
        OrderListVO orderListVO = new OrderListVO();

        Integer page = listBO.getPage();
        Integer limit = listBO.getLimit();
        String order = listBO.getOrder();
        String sort = listBO.getSort();
        String orderSn = listBO.getOrderSn();
        Integer orderStatusArray = listBO.getOrderStatusArray();
        Integer userId = listBO.getUserId();

        //分页信息
        PageHelper.startPage(page,limit);
        PageInfo<OrderListVO> orderListVOPageInfo = new PageInfo<>();
        long total = orderListVOPageInfo.getTotal();

        //排序信息
        MallOrderExample orderExample = new MallOrderExample();
        orderExample.setOrderByClause(sort + " " + order);
        MallOrderExample.Criteria criteria = orderExample.createCriteria();

        //查询
        criteria.andDeletedEqualTo(false);
        if (!"0".equals(orderSn) && orderSn != null){
//            criteria.andOrderSnEqualTo(orderSn);
            criteria.andOrderSnLike("%" + orderSn + "%");
        }
        if (orderStatusArray != null && orderStatusArray != 0){
            criteria.andOrderStatusEqualTo(orderStatusArray);
        }
        if (userId != null && userId != 0){
            criteria.andUserIdEqualTo(userId);
        }

        List<MallOrder> mallOrders = orderMapper.selectByExample(orderExample);

        orderListVO.setTotal(total);
        orderListVO.setItems(mallOrders);

        return orderListVO;
    }
}